<template>
  <el-dialog
    title="上报工作日志"
    :close-on-click-modal="false"
    draggable
    v-model="open"
    width="70%"
    append-to-body
  >
    <logContent ref="logForm" :isForm="true" @history="openHistory"></logContent>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" v-if="logForm?.prodWorkStationChangeLog" @click="submitForm" v-hasPermi="['ProdWorkStationChangeLog:ProdWorkStationChangeLog:edit']">确 定</el-button>
        <el-button type="primary" v-else @click="submitForm"  v-hasPermi="['ProdWorkStationChangeLog:ProdWorkStationChangeLog:add']">确 定</el-button>

        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="MatDialog">
import { addLog, editLog } from "@/api/productionMgt/changeShifts";
import logContent from "./logContent.vue";
import { nextTick, ref, watch, getCurrentInstance } from "vue";
const { proxy } = getCurrentInstance();
const open = defineModel("show");
const logForm = ref();

const emit = defineEmits(["change", "history"]);

watch(
  () => open.value,
  (newValue) => {
    if (newValue) {
      nextTick(() => {
        logForm.value.reset();
        logForm.value.getDeptInfo();
        logForm.value.setProdDateFun();
      });
    }
  }
);

/** 取消按钮 */
function cancel() {
  open.value = false;
}

function submitForm() {
  const params = logForm.value.form;
  if (logForm.value.prodWorkStationChangeLog) {
    editLog(params).then(() => {
      proxy.$modal.msgSuccess("提交成功");
      open.value = false;
    });
  } else {
    addLog(params).then(() => {
      proxy.$modal.msgSuccess("提交成功");
      open.value = false;
    });
  }
}
function openHistory() {
  emit("history");
}
</script>

<style lang="scss" scoped></style>
